﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.成品出库单.service;
using DXApplicationTangche.UC.盘点.Service;
using System.Diagnostics;

namespace DXApplicationTangche.UC.盘点.盘点UC
{
    public partial class XtraUserControl门店盘点计划 : DevExpress.XtraEditors.XtraUserControl
    {
        private StockStoreInventoryPlanDTO stockStoreInventoryPlanDTO = new StockStoreInventoryPlanDTO();
        private StockInOutDetailDTO stockInOutDetailDTO = new StockInOutDetailDTO();
        private String barcode;//扫码
        private String stockInventoryPlanCode;//盘点大计划号
        private String stockStoreInventoryPlanCode;//门店盘点计划号
        public XtraUserControl门店盘点计划()
        {
            InitializeComponent();
        }
        public XtraUserControl门店盘点计划(String stockInventoryPlanCode, String stockStoreInventoryPlanCode)
        {
            //传参 单击行的盘点计划id，调接口绑定数据源
            InitializeComponent();
            this.stockInventoryPlanCode = stockInventoryPlanCode;
            this.stockStoreInventoryPlanCode = stockStoreInventoryPlanCode;
            this.stockInOutDetailDTO = new StockInOutDetailDTO();
            this.refreshDate();
        }
        public void refreshDate()
        {
            //扫完的成衣信息赋值给gridcontrol
            this.stockStoreInventoryPlanDTO = 盘点Service.getStockStoreInventoryPlanByCode(this.stockStoreInventoryPlanCode);
            this.gridControl盘点成衣信息.DataSource = this.stockStoreInventoryPlanDTO.stockInOutDetailDTOS;
            this.gridView盘点成衣信息.RefreshData();
            this.textBox扫描条码.Enabled = !stockStoreInventoryPlanDTO.isDone;

        }
        //盘点完成
        private void simpleButton盘点完成_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定完成吗？", "门店计划完成", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                splashScreenManager.ShowWaitForm();
                splashScreenManager.SetWaitFormCaption("请稍后,正在完成中....");     // 标题
                splashScreenManager.SetWaitFormDescription("正在完成.....");     // 信息
                盘点Service.saveStockStoreInventoryPlanDone(
                    this.stockInventoryPlanCode,
                    this.stockStoreInventoryPlanCode
                    );
                this.refreshDate();
                splashScreenManager.CloseWaitForm();
            }
        }
        //扫码
        private void textBox扫描条码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox扫描条码.Text.Trim()))
                {
                    MessageBox.Show("请输入正确条码");
                    return;
                }
                this.barcode = this.textBox扫描条码.Text.Trim();
                this.stockInOutDetailDTO = 盘点Service.saveStockInventoryDetail(this.stockStoreInventoryPlanCode, this.barcode);
                if (String.IsNullOrEmpty(this.stockInOutDetailDTO.barcode))
                {
                    MessageBox.Show("录入失败请检查货号");
                    this.textBox扫描条码.SelectAll();
                    return;
                }
                else
                {
                    this.refreshDate();
                    this.textBox扫描条码.Clear();
                }
            }

        }

        private void simpleButton盘盈一览_Click(object sender, EventArgs e)
        {
            if (this.stockStoreInventoryPlanDTO.isDone)
            {
                new Form盘点完成(this.stockStoreInventoryPlanDTO).ShowDialog();
            }
            else
            {
                MessageBox.Show("门店盘点计划未完成不能做盘盈");
            }
        }

        private void simpleButton盘点完成Excel导出_Click(object sender, EventArgs e)
        {
            if (!this.stockStoreInventoryPlanDTO.isDone)
            {
                MessageBox.Show("当前盘点进行中");
                return;
            }
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            盘点Service.saveExcelFor盘点完成(
                this.splashScreenManager,
                this.stockStoreInventoryPlanDTO,
                盘点Service.getStockInventoryAll(stockStoreInventoryPlanCode, "盘盈"),
                盘点Service.getStockInventoryAll(stockStoreInventoryPlanCode, "盘亏"));
            this.splashScreenManager.CloseWaitForm();
            stopWatch.Stop();
            System.Diagnostics.Debug.WriteLine("====================Excel导出时间" + stopWatch.ElapsedMilliseconds + " ms=======================");
            MessageBox.Show("保存成功");
        }

        
    }
}